$(function () {
    $("#jqGrid").Grid({
        url: '../knowbase/list',
        colModel: [
            {label: '标题', name: 'title', index: 'TITLE', width: 80},
            {label: '设备/模具编码', name: 'devCode', index: 'DEV_CODE', width: 80},
            {label: '设备/模具名称', name: 'devName', index: 'DEV_NAME', width: 80},
            {
                label: '模块类型', name: 'type', index: 'TYPE', width: 60, formatter: function (value) {
                    return sysEnums.KNOW_TYPE3[value];
                }
            },
            {
                label: '是否通用', name: 'proType', index: 'PRO_TYPE', width: 60, formatter: function (value) {
                    if(value==null){
                        return "暂无"
                    }else{
                        return sysEnums.KNOW_TYPE4[value];
                    }
                }
            },
            {label: '知识归类', name: 'konwTypeName', index: 'KONW_TYPE', width: 60},
            {label: '标签', name: 'keyTitile', index: 'KEY_TITILE', width: 40},
            {label: '原因', name: 'reason', index: 'REASON', width: 80},
            // {label: '删除标志', name: 'isDel', index: 'IS_DEL', width: 80},
            // {label: '引用次数', name: 'quoteNum', index: 'QUOTE_NUM', width: 40},
            // {label: '阅读次数', name: 'readNum', index: 'READ_NUM', width: 40},
            {label: '内容', name: 'contents', index: 'CONTENTS', width: 80},
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {
                label: '附件', width: 80, align: 'center', sortable: false,
                formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' onclick='vm.goAnnexFile(\"" + row.id + "\")'><i class='fa fa-upload'></i>&nbsp;附件列表</button>";
                }
            }
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        sheb:true,
        knowBase: {},
        ruleValidate: {
            devCode: [
                {required: true, message: '设备编码不能为空', trigger: 'change'}
            ],
            title: [
                {required: true, message: '标题不能为空', trigger: 'blur'}
            ]
        },
        q: {
            title: '',
            keyTitile:'',
            proType:''
        },
        moduleTypeList: enumsToList('KNOW_TYPE3'), //模块类型
        proTypeList: enumsToList('KNOW_TYPE4'), //是否通用
        knowTypeList:[] //知识归类
    },
    methods: {
        onselect:function(e){
            if(e){
                vm.sheb = true
            }else{
                vm.sheb = false
            }
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.knowBase = {id: '', type: 1, konwType: 1, devCode: ''};
            vm.getKnowTypeList();
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getKnowTypeList();
            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.knowBase.id == (null || '') ? "../knowbase/save" : "../knowbase/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.knowBase),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../knowbase/changeDelCode",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../knowbase/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.knowBase = r.knowBase;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'title': vm.q.title,'keyTitile':vm.q.keyTitile, 'proType':vm.q.proType},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                title: '',
                keyTitile:'',
                proType:''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        goAnnexFile: function (id) {
            openWindow({
                type: 2,
                area: ['100%', '100%'],
                title: '<i class="fa fa-list-ul"></i> 附件列表',
                content: '../sys/annexfile.html?id=' + id
            })
        },
        //显示设备列表-使用设备码
        getDeviceInfo:function() {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择设备',
                content: '../sys/deviceInfoSelect.html',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getDeviceInfo();
                    if(!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        layer.close(index);
                        vm.knowBase.devCode = res.devCode;
                        vm.knowBase.devName = res.devName;
                    }
                }
            })
        },

        getKnowTypeList: function () {
            Ajax.request({
                url: "../sysknowtype/queryAll" ,
                async: true,
                successCallback: function (r) {
                    vm.knowTypeList = r.list;
                }
            });
        },
    }
});

var getKnowbaseId = function () {
    return getSelectedRow('#jqGrid');
}
